package com.quanshu.exchange.ui.gui.profile.order_fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.DensityUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.gui.profile.AllOrderActivity;
import com.quanshu.shop.base.view.BaseTitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RefundReasonActivity extends BaseActivity {

    @BindView(R.id.rg_reason)
    RadioGroup rgReason;
    @BindView(R.id.ok_btn)
    Button okBtn;
    @BindView(R.id.title)
    BaseTitleBar title;

    private List<String> stringList;
    private String reason;
    private String orderId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_reason);
        ButterKnife.bind(this);
        ImmersionBar.with(this).titleBar(title).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        initIntent();
        initData();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initIntent() {
        orderId = getIntent().getStringExtra("orderId");
    }

    private void initView() {
        if (stringList != null && stringList.size() > 0) {
            for (int i = 0; i < stringList.size(); i++) {
                RadioButton radioButton = new RadioButton(this);
                radioButton.setId(i);
                radioButton.setBackground(null);
                radioButton.setButtonDrawable(R.drawable.selector_radiobtn_bg);
                radioButton.setPadding(DensityUtil.dp2px(this, 10),
                        (DensityUtil.dp2px(this, 10)),
                        (DensityUtil.dp2px(this, 10)),
                        (DensityUtil.dp2px(this, 10)));
                radioButton.setText(stringList.get(i));
                radioButton.setTextColor(getResources().getColor(R.color.black));
                rgReason.addView(radioButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            }
        }
    }

    private void initListener() {
        title.setCenterText("退款原因").setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        rgReason.setOnCheckedChangeListener((group, checkedId) -> {
            RadioButton tempButton = findViewById(checkedId);
            reason = tempButton.getText().toString();
        });
        RxViewHelper.getInstance().clicks(okBtn, aVoid -> doRefund());
    }

    private void doRefund() {
        if (reason == null || reason.length() < 1) {
            ToastHelper.toast("请选择退款原因");
            return;
        }
        NetClient.getInstance()
                .getShopService(ShopInterface.class)
                .doRefund(orderId, reason)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        Intent intent = new Intent(RefundReasonActivity.this, AllOrderActivity.class);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
    }

    private void initData() {
        NetClient.getInstance()
                .getShopService(ShopInterface.class)
                .getRefundReason()
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<String>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<String>> response) {
                        stringList = response.getData();
                        initView();
                    }
                });
    }
}
